package com.abc.robot01.activity.compose

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.filled.ThumbUp
import androidx.compose.material3.Button
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.PrimaryTabRow
import androidx.compose.material3.ScrollableTabRow
import androidx.compose.material3.SecondaryTabRow
import androidx.compose.material3.Surface
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import androidx.navigation.ui.AppBarConfiguration
import com.abc.robot01.databinding.ActivityMainBinding

//https://github.com/android/compose-samples
//https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/TabSamples.kt
//https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/DatePickerSamples.kt

class TabActivity02 : ComponentActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding


    @Composable
    fun BasicTabs() {
        val tabTitles = listOf("Tab 1", "Tab 2", "Tab 3")
        var selectedTabIndex by remember { mutableStateOf(0) }

        Column {
            TabRow(selectedTabIndex = selectedTabIndex) {
                tabTitles.forEachIndexed { index, title ->
                    Tab(
                        selected = selectedTabIndex == index,
                        onClick = { selectedTabIndex = index },
                        text = { Text(title) }
                    )
                }
            }
            when(selectedTabIndex){
                     0 -> {
                         Text(text = "Selected tab: ${tabTitles[selectedTabIndex]}")
                     }
                1 -> {
                    Button(onClick = { /*TODO*/ }) {
                        Text(text = "111")

                    }
                }
                else -> {
                    BasicOutlinedTextField02()
                }
            }
        }
    }

    @Composable
    fun ScrollableTabs() {
        val tabTitles = listOf("Tab 1", "Tab 2", "Tab 3", "Tab 4", "Tab 5", "Tab 6")
        var selectedTabIndex by remember { mutableStateOf(0) }

        Column {
            ScrollableTabRow(selectedTabIndex = selectedTabIndex) {
                tabTitles.forEachIndexed { index, title ->
                    Tab(
                        selected = selectedTabIndex == index,
                        onClick = { selectedTabIndex = index },
                        text = { Text(title) }
                    )
                }
            }
            Text(text = "Selected tab: ${tabTitles[selectedTabIndex]}")
        }
    }



    @Composable
    fun BasicOutlinedTextField02() {
        val text = remember { mutableStateOf("你好") }
        OutlinedTextField(
            isError =  true,
            value = text.value,
            onValueChange = { text.value = it },
        )
    }

    @Composable
    fun TabsWithIcons() {
        val tabTitles = listOf("Home", "Search", "Settings")
        val tabIcons = listOf(Icons.Default.Home, Icons.Default.Search, Icons.Default.Settings)
        var selectedTabIndex by remember { mutableStateOf(0) }

        Column {
            TabRow(selectedTabIndex = selectedTabIndex) {
                tabTitles.forEachIndexed { index, title ->
                    Tab(
                        selected = selectedTabIndex == index,
                        onClick = { selectedTabIndex = index },
                        text = { Text(title) },
                        icon = { Icon(imageVector = tabIcons[index], contentDescription = title) }
                    )
                }
            }
            Text(text = "Selected tab: ${tabTitles[selectedTabIndex]}")
        }
    }

    @Composable
    fun CustomTabContent() {
        val tabTitles = listOf("Tab 1", "Tab 2", "Tab 3")
        var selectedTabIndex by remember { mutableStateOf(0) }

        Column {
            TabRow(selectedTabIndex = selectedTabIndex) {
                tabTitles.forEachIndexed { index, title ->
                    Tab(
                        selected = selectedTabIndex == index,
                        onClick = { selectedTabIndex = index }
                    ) {
                        Column(horizontalAlignment = Alignment.CenterHorizontally) {
                            Icon(Icons.Default.Star, contentDescription = title)
                            Text(text = title)
                        }
                    }
                }
            }
            Text(text = "Selected tab: ${tabTitles[selectedTabIndex]}")
        }
    }

    @Composable
    fun TabsWithBackgroundColor() {
        val tabTitles = listOf("Tab 1", "Tab 2", "Tab 3")
        var selectedTabIndex by remember { mutableStateOf(0) }

        Column {
            TabRow(
                selectedTabIndex = selectedTabIndex,
//                contentColor = Color.White
            ) {
                tabTitles.forEachIndexed { index, title ->
                    Tab(
                        selected = selectedTabIndex == index,
                        onClick = { selectedTabIndex = index },
                        text = { Text(title) }
                    )
                }
            }
            Text(text = "Selected tab: ${tabTitles[selectedTabIndex]}")
        }
    }


    @OptIn(ExperimentalMaterial3Api::class)
    @Composable
    fun PrimaryTextTabs() {
        var state by remember { mutableStateOf(0) }
        val titles = listOf("Tab 1", "Tab 2", "Tab 3 with lots of text")
        Column {
            PrimaryTabRow(selectedTabIndex = state) {
                titles.forEachIndexed { index, title ->
                    Tab(
                        selected = state == index,
                        onClick = { state = index },
                        text = { Text(text = title, maxLines = 2, overflow = TextOverflow.Ellipsis) }
                    )
                }
            }
            Text(
                modifier = Modifier.align(Alignment.CenterHorizontally),
                text = "Primary tab ${state + 1} selected",
                style = MaterialTheme.typography.bodyLarge
            )
        }
    }

    @Composable
    @OptIn(ExperimentalMaterial3Api::class)
    fun PrimaryIconTabs() {
        var state by remember { mutableStateOf(0) }
        val icons = listOf(Icons.Filled.Favorite, Icons.Filled.Favorite, Icons.Filled.Favorite)
        Column {
            PrimaryTabRow(selectedTabIndex = state) {
                icons.forEachIndexed { index, icon ->
                    Tab(
                        selected = state == index,
                        onClick = { state = index },
                        icon = { Icon(icon, contentDescription = "Favorite") }
                    )
                }
            }
        }
    }

//    @Composable
//    @OptIn(ExperimentalMaterial3Api::class)
//    fun FancyIndicatorTabs() {
//        var state by remember { mutableStateOf(0) }
//        val titles = listOf("Tab 1", "Tab 2", "Tab 3")
//
//        Column {
//            SecondaryTabRow(
//                selectedTabIndex = state,
//                indicator = {
//                    FancyIndicator(
//                        MaterialTheme.colorScheme.primary,
//                        Modifier.tabIndicatorOffset(state)
//                    )
//                }
//            ) {
//                titles.forEachIndexed { index, title ->
//                    Tab(selected = state == index, onClick = { state = index }, text = { Text(title) })
//                }
//            }
//            Text(
//                modifier = Modifier.align(Alignment.CenterHorizontally),
//                text = "Fancy indicator tab ${state + 1} selected",
//                style = MaterialTheme.typography.bodyLarge
//            )
//        }
//    }

    @Composable
    fun FancyTab(title: String, onClick: () -> Unit, selected: Boolean) {
        Tab(selected, onClick) {
            Column(
                Modifier.padding(10.dp).height(50.dp).fillMaxWidth(),
                verticalArrangement = Arrangement.SpaceBetween
            ) {
                Box(
                    Modifier.size(10.dp)
                        .align(Alignment.CenterHorizontally)
                        .background(
                            color =
                            if (selected) MaterialTheme.colorScheme.primary
                            else MaterialTheme.colorScheme.background
                        )
                )
                Text(
                    text = title,
                    style = MaterialTheme.typography.bodyLarge,
                    modifier = Modifier.align(Alignment.CenterHorizontally)
                )
            }
        }
    }

    @Composable
    fun FancyTab2(title: String, onClick: () -> Unit, selected: Boolean) {
        Tab(selected, onClick) {
            Icon(
                imageVector = Icons.Filled.ThumbUp,
                contentDescription = "Like",
                tint = MaterialTheme.colorScheme.secondary
            )
        }
    }

    @Composable
    @OptIn(ExperimentalMaterial3Api::class)
    fun FancyTabs() {
        var state by remember { mutableStateOf(0) }
        val titles = listOf("Tab 1", "Tab 2", "Tab 3")
        Column {
            SecondaryTabRow(selectedTabIndex = state) {
                titles.forEachIndexed { index, title ->
                    FancyTab(title = title, onClick = { state = index }, selected = (index == state))
                }
            }
            Text(
                modifier = Modifier.align(Alignment.CenterHorizontally),
                text = "Fancy tab ${state + 1} selected",
                style = MaterialTheme.typography.bodyLarge
            )
        }
    }

    @Composable
    @OptIn(ExperimentalMaterial3Api::class)
    fun FancyTabs2() {
        var state by remember { mutableStateOf(0) }
        val titles = listOf("Tab 1", "Tab 2", "Tab 3")
        Column {
            SecondaryTabRow(selectedTabIndex = state, indicator = {}) {
                titles.forEachIndexed { index, title ->
                    FancyTab(title = title, onClick = { state = index }, selected = (index == state))
                }
            }
            Text(
                modifier = Modifier.align(Alignment.CenterHorizontally),
                text = "Fancy tab ${state + 1} selected",
                style = MaterialTheme.typography.bodyLarge
            )
        }
    }

    @Composable
    @OptIn(ExperimentalMaterial3Api::class)
    fun FancyTabs3() {
        var state by remember { mutableStateOf(0) }
        val titles = listOf("Tab 1", "Tab 2", "Tab 3")
        Column {
            SecondaryTabRow(selectedTabIndex = state, indicator = {}) {
                titles.forEachIndexed { index, title ->
                    FancyTab2(title = title, onClick = { state = index }, selected = (index == state))
                }
            }
            Text(
                modifier = Modifier.align(Alignment.CenterHorizontally),
                text = "Fancy tab ${state + 1} selected",
                style = MaterialTheme.typography.bodyLarge
            )
        }
    }

    @Composable
    fun FancyIndicator(color: Color, modifier: Modifier = Modifier) {
        // Draws a rounded rectangular with border around the Tab, with a 5.dp padding from the edges
        // Color is passed in as a parameter [color]
        Box(
            modifier
                .padding(5.dp)
                .fillMaxSize()
                .border(BorderStroke(2.dp, color), RoundedCornerShape(5.dp))
        )
    }

    @Composable
    @OptIn(ExperimentalMaterial3Api::class)
    fun FancyIndicatorTabs() {
        var state by remember { mutableStateOf(0) }
        val titles = listOf("Tab 1", "Tab 2", "Tab 3")

        Column {
            SecondaryTabRow(
                selectedTabIndex = state,
                indicator = {
                    FancyIndicator(
                        MaterialTheme.colorScheme.primary,
                        Modifier.tabIndicatorOffset(state)
                    )
                }
            ) {
                titles.forEachIndexed { index, title ->
                    Tab(selected = state == index, onClick = { state = index }, text = { Text(title) })
                }
            }
            Text(
                modifier = Modifier.align(Alignment.CenterHorizontally),
                text = "Fancy indicator tab ${state + 1} selected",
                style = MaterialTheme.typography.bodyLarge
            )
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("FirstFragment","MainActivity  onCreate");
        setContent {
            MaterialTheme {
                Surface() {
                    val openDialog = remember { mutableStateOf(true) }

                    Column( modifier = Modifier
                        .fillMaxSize()
                        .verticalScroll(rememberScrollState())
                        .padding(8.dp),
                        verticalArrangement = Arrangement.spacedBy(8.dp)) {


                        FancyIndicatorTabs()

                        FancyTabs3()

                        FancyTabs()
                        FancyTabs2()



                        PrimaryIconTabs()
                        PrimaryTextTabs()



                        BasicTabs()
                        ScrollableTabs()
                        TabsWithIcons()
                        CustomTabContent()
                        TabsWithBackgroundColor()
//                        DateInputSample()

//                        Button(onClick = {
//                            openDialog.value = true
//                        } ,
//                        ){ Text("Click Me")}
                        //ok
//                        DatePickerDialogSample(openDialog)
                        //ok
//                        DatePickerWithDateSelectableDatesSample()
                        //ok
//                        DatePickerSample()
                        //ok
//                        DatePickerModal();

//
//                        KeyboardTypeNumberPassword()
//                        KeyboardTypeNumberPassword02()
//
//                        OutlinedTextFieldWithTrailingIcon()
//                        OutlinedTextFieldWithLeadingIcon()
//                        BasicOutlinedTextField()
//                        BasicOutlinedTextField02()
//                        BasicOutlinedTextField03()
//                        OutlinedTextFieldWithError()
//                        OutlinedTextFieldWithPlaceholder()
//                        OutlinedTextFieldWithCustomTextStyle()
//                        KeyboardTypeNumber()
//                        KeyboardTypePhone()
//
//                        CustomIconButtonDemo()
//
//                        IconWithoutTintDemo()
//                        IconWithTintDemo()
//
//                        IconButtonDemo{
//                        }
//
//                        DatePickerDocked()
//
//
//
//                        BasicPopupDemo()
//                        BasicPopupDemo02()
//                        BasicPopupDemo03()
//                        LazyColumn01()
//                        BasicLazyColumn()
//                        Row{
//                            LazyColumn02()
//                            LazyColumn03()
//                            LazyColumn04()
//                            LazyColumn05()
//                        }
//
//                        CustomColorAndStrokeCircularProgressIndicator()
//                        DeterminateCircularProgressIndicatorExample()
//                        BasicAlertDialogExample()
//                        CustomSurface()
//                        ContentColor()
//                        ContentColor2()
//                        ShadowElevation0()
//                        ShadowElevation01()
//                        ShadowElevation02()
//                        ClickableCard{}
//                        Spacer(modifier = Modifier.height(20.dp))
//                        BasicSliderExample()
//                        BasicSwitchExample()
//
//                        Text1()
//                        Text2()
//                        CardExample(0)
//                        CardExample(1)
//                        CheckboxExample()
//                        TextFieldExample()
//                        StyledText()
//                        RowLayout()
//                        SimpleButton()
//                        DarkColorSchemeButton()
//                        LightColorSchemeButton()
//                        SimpleCanvasDrawCircle()
//                        SimpleCanvasDrawRect()
//                        LineAndPathDrawingCanvas()
//                        AnimatedCanvas()
//
//                        CircularProgressIndicatorExample()
                    }
                }
            }
        }





//        binding = ActivityMainBinding.inflate(layoutInflater)
//        setContentView(binding.root)
//
//        setSupportActionBar(binding.toolbar)


//        if (savedInstanceState == null) {
//            supportFragmentManager.beginTransaction()
//                .add(R.id.fragment_container, a, "1")
//                .commit()
//        }

//        val navController = findNavController(R.id.nav_host_fragment_content_main)
//        appBarConfiguration = AppBarConfiguration(navController.graph)
//        setupActionBarWithNavController(navController, appBarConfiguration)

//        binding.fab.setOnClickListener { view ->
////            replaceFragment(b,"2");
////            val fragmentManager: FragmentManager = supportFragmentManager
////            val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
////            fragmentTransaction.hide(b)
////            fragmentTransaction.show(a);
////            fragmentTransaction.commit();
//
//
//            val fragmentManager = supportFragmentManager
//            val fragmentTransaction = fragmentManager.beginTransaction()
//            fragmentTransaction.replace(R.id.fragment_container, a, "1")
//            fragmentTransaction.addToBackStack("1")
//            fragmentTransaction.commit()




//            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                .setAction("Action", null)
//                .setAnchorView(R.id.fab).show()
//            try{
//                navController.navigate(R.id.action_FirstFragment_to_Fragment03);
//
//            }catch (_:Exception){
//
//            }

//            Log.e("111",""+navController.currentDestination?.displayName);






//            navController.findDestination("1111").


        }


//        binding.fab2.setOnClickListener { view ->
////            replaceFragment(a,"1");
////            val fragmentManager: FragmentManager = supportFragmentManager
////            val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
////            fragmentTransaction.hide(a)
////            fragmentTransaction.commit();
//
//
//            val fragmentManager = supportFragmentManager
//            val fragmentTransaction = fragmentManager.beginTransaction()
//            fragmentTransaction.replace(R.id.fragment_container, b, "2")
//            fragmentTransaction.addToBackStack("2")
//            fragmentTransaction.commit()


//            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                .setAction("Action", null)
//                .setAnchorView(R.id.fab).show()
//            try{
//                navController.navigate(R.id.action_FirstFragment_to_Fragment03);
//
//            }catch (_:Exception){
//
//            }

//            Log.e("111",""+navController.currentDestination?.displayName);






//            navController.findDestination("1111").


//        }
//    }


//    fun replaceFragment(fragment: Fragment, tag: String) {
//        val fragmentManager: FragmentManager = supportFragmentManager
//        val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
//        fragmentTransaction.replace(R.id.fragment_container, fragment, tag)
//        fragmentTransaction.addToBackStack(tag)
//        fragmentTransaction.commit()
//    }
//
//    override fun onCreateOptionsMenu(menu: Menu): Boolean {
//        // Inflate the menu; this adds items to the action bar if it is present.
//        menuInflater.inflate(R.menu.menu_main, menu)
//        return true
//    }
//
//    override fun onOptionsItemSelected(item: MenuItem): Boolean {
//        // Handle action bar item clicks here. The action bar will
//        // automatically handle clicks on the Home/Up button, so long
//        // as you specify a parent activity in AndroidManifest.xml.
//        return when (item.itemId) {
//            R.id.action_settings -> true
//            else -> super.onOptionsItemSelected(item)
//        }
//    }

//    override fun onSupportNavigateUp(): Boolean {
////        val navController = findNavController(R.id.nav_host_fragment_content_main)
////        return navController.navigateUp(appBarConfiguration)
////                || super.onSupportNavigateUp()
//    }

    companion object{
//        val  a = FirstFragment();
//        val  b = SecondFragment();
    }
}